import 'package:get/get.dart';
import 'package:hygf/bean/chart_bean.dart';
import 'package:hygf/http/core/ResponseData.dart';

import '../../../config/ApiConfig.dart';
import '../../../http/DioClient.dart';
import 'state.dart';

class DataLogic extends GetxController {
  final DataState state = DataState();

  @override
  void onInit() {
    super.onInit();
    requestList(true);
    requestChart(false);
    requestChartForPower(false);
  }

  void requestList(bool isRefresh,
      {String? selectedType, String? searchKey}) async {
    ResponseData response = await DioClient().doGet(
      ApiConfig.stationStatus,
      queryParameters: null,
    );
    if (response.code == "0") {
      if (response.data != null) {
        state.online = response.data["online"];
        state.alarm = response.data["alarm"];
        state.offline = response.data["offline"];
        state.total = state.online + state.alarm + state.offline;
        if (state.alarm != 0) {
          state.alarmWidth = (state.alarm / state.total * 160).toInt();
          if(state.alarmWidth < 50) {
            state.alarmWidth = 50;
          }
        }
        if (state.offline != 0) {
          state.offlineWidth = (state.offline / state.total * 160).toInt();
          if(state.offlineWidth < 50) {
            state.offlineWidth = 50;
          }
        }
        update();
      }
    }
  }

  void requestChart(bool isRefresh,
      {String? selectedType, String? searchKey}) async {
    Map<String, dynamic> data = {
      "attribId": state.isMonth == 0 ? "201" : "202",
      "stime": state.isMonth == 0
          ? ("${state.year}-${state.month.toString().padLeft(2, '0')}")
          : state.year.toString(),
    };
    ResponseData response = await DioClient().doGet(
      ApiConfig.elecChart,
      queryParameters: data,
    );
    if (response.code == "0") {
      if (response.data != null) {
        state.chartList.clear();
        response.data.forEach((v) {
          state.chartList.add(ChartBean.fromJson(v));
        });
        state.barMaxValue = findMaxAndRound(state.chartList);
      }
    }
    update();
  }

  void requestChartForPower(bool isRefresh,
      {String? selectedType, String? searchKey}) async {
    Map<String, dynamic> data = {
      "attribId":  "201" ,
      "stime": ("${DateTime.now().year}-${DateTime.now().month.toString().padLeft(2, '0')}"),
    };
    ResponseData response = await DioClient().doGet(
      ApiConfig.elecChart,
      queryParameters: data,
    );
    if (response.code == "0") {
      if (response.data != null) {
        response.data.forEach((v) {
          ChartBean bean = ChartBean.fromJson(v);
            state.todayPower = bean.attribValue??0;
        });
      }
    }
    Map<String, dynamic> dataYear = {
      "attribId":  "202",
      "stime":  DateTime.now().year.toString(),
    };
    ResponseData responseYear = await DioClient().doGet(
      ApiConfig.elecChart,
      queryParameters: dataYear,
    );
    if (responseYear.code == "0") {
      if (responseYear.data != null) {
        responseYear.data.forEach((v) {
          ChartBean bean = ChartBean.fromJson(v);
          state.monthPower = bean.attribValue??0;
        });
      }
    }
    update();
  }

  void getYearData(String s) {
    if (s == "add") {
      if (state.isMonth == 0) {
        state.month = state.month + 1;
        if (state.month > 12) {
          state.year = state.year + 1;
          state.month = 1;
        }
      } else {
        state.year = state.year + 1;
      }
    } else {
      if (state.isMonth == 0) {
        state.month = state.month - 1;
        if (state.month < 1) {
          state.year = state.year - 1;
          state.month = 12;
        }
      } else {
        state.year = state.year - 1;
      }
    }
    requestChart(false);
  }

  // 找出最大数并取整的函数
  int findMaxAndRound(List<ChartBean> chartBeans) {
    double maxValue = double.negativeInfinity;
    for (var bean in chartBeans) {
      if (bean.attribValue != null && bean.attribValue! > maxValue) {
        maxValue = bean.attribValue!;
      }
      if (bean.earnValue != null && bean.earnValue! > maxValue) {
        maxValue = bean.earnValue!;
      }
    }
    return maxValue.round();
  }
}
